package com.qianseit.westore.activity;

import org.json.JSONObject;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;

import com.qianseit.westore.BaseDoFragment;
import com.qianseit.westore.Run;
import com.qianseit.westore.http.JsonRequestBean;
import com.qianseit.westore.http.JsonTask;
import com.qianseit.westore.http.JsonTaskHandler;
import com.beautymiracle.androidclient.R;

/**
 * 关于、帮助
 * 
 * 
 */
public class FeedbackFragment extends BaseDoFragment implements OnClickListener {
	private EditText mPhoneText, mContentText;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mActionBar.setTitle(R.string.feedback);
	}

	@Override
	public void init(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_feedback, null);
		mPhoneText = (EditText) findViewById(R.id.feedback_phone_number);
		mContentText = (EditText) findViewById(R.id.feedback_content);
		findViewById(R.id.feedback_submit).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (R.id.feedback_submit == v.getId()) {
			if (TextUtils.isEmpty(mContentText.getText())
					|| TextUtils.isEmpty(mPhoneText.getText()))
				return;
			Run.excuteJsonTask(new JsonTask(), new FeedbackTask());
		} else {
			super.onClick(v);
		}
	}

	private class FeedbackTask implements JsonTaskHandler {
		@Override
		public JsonRequestBean task_request() {
			showCancelableLoadingDialog();
			return new JsonRequestBean(Run.API_URL, "mobileapi.member.send_msg")
					.addParams("comment", mContentText.getText().toString())
					.addParams("subject", mPhoneText.getText().toString());
		}

		@Override
		public void task_response(String json_str) {
			hideLoadingDialog_mt();

			try {
				JSONObject all = new JSONObject(json_str);
				if (Run.checkRequestJson(mActivity, all)) {
					Run.alert(mActivity, R.string.feedback_success);
					mActivity.finish();
				}
			} catch (Exception e) {
			}
		}
	}

}
